//
//  CircleSegment.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/17/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CircleSegment.h"
#import "PhysicSystem.h"
#import "PhysicBody.h"
#import "Bullet.h"
#import "Circle.h"

@implementation CircleSegment
@synthesize _type = mType, _color = mColorType;

-(id) initWithType:(SEGMENT_TYPE)type color:(COLOR_TYPE)color radius:(float)radius
{
    mType = type;
    mColorType = color;
    mRadius = radius;
    mIsDestroy = NO;
    mIsCheckCollision = NO;
    
    if (self = [super initWithSpriteFrameName:[self getSpriteFrameName]])
    {
        [self initPhysicBody];
        [self initRatio];
    }
    return self;
}

-(void) initPhysicBody
{
    PhysicBody *body = [[PhysicBody alloc] initWithDelegate:self shapeType:TYPE_CIRCLE value:mRadius];
    [[PhysicSystem sharedPhysicSystem] addBodyToSystem:body withType:TYPE_PHYSIC_OBJECT_CIRCLE];
    
    [body release];
}

-(void) initRatio
{
    float ratio = mRadius / (self.contentSize.width / 2);
    [self setScale:ratio];
}

-(NSString *) getSpriteFrameName
{
    NSString *frameName;
    switch (mType) {
            
        case TYPE_COLOR:
            switch (mColorType) {
                case TYPE_COLOR_RED:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_RED;
                    break;
                    
                case TYPE_COLOR_BLUE:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_BLUE;
                    break;
                    
                case TYPE_COLOR_GREEN:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_GREEN;
                    break;
                    
                case TYPE_COLOR_PURPLE:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_PURPLE;
                    break;
                    
                case TYPE_COLOR_YELLOW:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_YELLOW;
                    break;
                    
                default:
                    frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_RED;
                    break;
            }
            break;
        
        case TYPE_GREEN_PLANET:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_BLUE;
            break;
            
        case TYPE_SHIELD:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_BLUE;
            break;
        
        case TYPE_ROCK_1:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_BLUE;
            break;
            
        case TYPE_ROCK_2:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_BLUE;
            break;
            
        case TYPE_ROCK_3:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_BLUE;
            break;
            
        default:
            frameName = FRAME_NAME_CIRCLE_SEGMENT_COLOR_RED;
            break;
    }
    
    return frameName;
}

-(void) beginContact:(id)otherContact
{
    if ([otherContact isKindOfClass:[Bullet class]])
    {
        Bullet *bullet = (Bullet *)(otherContact);
        if (bullet._type == TYPE_NORMAL)
        {
            if (bullet._colorType == mColorType)
            {
                [self destroySegment];
            }
        }
        
        Circle *circle = (Circle *)(self.parent.parent); // for test
        [circle contactWithBulletType:0];
    }
}

-(void) destroySegment
{
    [self setVisible:NO];
    mIsCheckCollision = NO;
    mIsDestroy = YES;
}

-(BOOL) isCheckCollision
{
    return mIsCheckCollision;
}

-(BOOL) isDestroy
{
    return mIsDestroy;
}

-(void) setCheckCollision:(BOOL)b
{
    mIsCheckCollision = b;
}

-(void) dealloc
{
    [super dealloc];
}

@end
